package com.hongdee.crm.average.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.crm.average.entity.TCrmAverageEntity;
import com.hongdee.crm.average.service.ITCrmIAverageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
@RestController
@RequestMapping("/crm/average")
/**
 * Created by d‘ell on 2017/5/26.
 */
public class AverageController {
    @Autowired
    private ITCrmIAverageService itCrmIAverageService;

    /**
     * @paramuid      登录用户 ID
     * @param tCrmAverageEntity 实体
     * @return tCrmAverageEntity
     * @throws Exception
     * @desc 新增与更改 保存数据
     * @author 于扬
     */

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmAverageEntity tCrmAverageEntity) throws Exception {
        EntityUtils.valid(tCrmAverageEntity);
        TCrmAverageEntity group = itCrmIAverageService.saveOrUpdate(tCrmAverageEntity);
        ResultEntity res = new ResultEntity();
        res.setData(group);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * @param id    用户 ID
     * @return tCrmAverageEntity
     * @throws Exception
     * @desc 按条件查询
     * @author 于扬
     */

    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        EntityUtils.valid(id);
        ResultEntity res = new ResultEntity();
        Map map =itCrmIAverageService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @paramuid  登录用户 ID
     * @param start
     * @param limit
     * @return tCrmAverageEntity
     * @throws Exception
     * @desc 分页查询
     * @author 于扬
     */

    @RequestMapping("/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "year",defaultValue = "")String year
    ) throws Exception {
        Map map = this.itCrmIAverageService.newPage(start, limit,year);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    @RequestMapping("/savesql")
    @ResponseBody
    public Map savesql(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "baseTime", defaultValue = "") String baseTime) throws Exception {

       Map map=this.itCrmIAverageService.save(baseTime);
       map.put("SUCCESS",true);
       map.put("msg","执行成功");
       return map;
    }
}
